/** AjaxHandler class **/

function AjaxHandler(){	

	/** Private attributes **/
	// Ajax handler
	var handler = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
	var onCompleteCallback = null;
	var _self = this;

	
	/** Public attributes **/
	this.result = "";
	this.resultXML = null;
	
	/** Public methods **/
	// Called when ajax request is completed
	this.setOnCompleteCallback = function(callback){
		onCompleteCallback = callback;
	}

	// Send an ajax request	
	this.sendRequest = function(target, param){

		handler.open("GET", target + parseParam(param), true);
		handler.send();

		handler.onreadystatechange = function(){
			
			// On complete
			if (handler.readyState == 4 && handler.status == 200) {
				
				_self.result = handler.responseText;
				_self.resultXML = handler.responseXML;
				(onCompleteCallback != null) ? onCompleteCallback() : alert("onCompleteCallback not set");
			}
		}
	}
	
	
	/** Private methods **/
	// Convert associative array to parameters
	function parseParam(param){
	
		if (param == null) {
			return "";
		}
		
		var options = "?";
		
		for (var key in param) {
			options += key + "=" + param[key] + "&";
		}
		
		// Trim the last "&"
		options = options.substring(0, options.length - 1);

		return options;
	}
}
